$(function () {
    // TODO 修改地址
    $('.change').click(function () {
        content = '<div class="shrtm">';
        content += '<span>收货人</span>';
        content += '<input type="text" class="addr_name">';
        content += '</div>';
        content += '<div class="shrszdq">';
        content += '<span>所在地区</span>';
        content += '<input type="text" class="addr_area">';
        content += '</div>';
        content += '<div class="shrjddz">';
        content += '<span>街道地址</span>';
        content += '<input type="text" class="addr_detail">';
        content += '</div>';
        content += '<div class="shrjbdh">';
        content += '<span>联系方式</span>';
        content += '<input type="text" class="telephone">';
        content += '</div>';
        content += '<div class="shrtmdcz" style=" float:right">';
        content += '<span>操作</span>';
        content += '<a href="javascript:;" class="addr_save" onclick="change_addr()">保存</a>';
        content += '</div>';
        $(this).parent().parent().html(content)

    })

    //  TODO 删除地址
    $('.remove').click(function () {
        var addr_id = $('.remove').parent().parent().prev().html();
        alert(addr_id)
        params = {
            'addr_id': addr_id,
        }
        $.ajax({
            url: '/user/remove_addr',
            type: "post",
            headers: {
                "X-CSRFToken": getCookie("csrf_token")
            },
            contentType: "application/json",
            data: JSON.stringify(params),
            success: function (data) {
                if (data.errno == '0') {
                    alert(data.errmsg)
                    content = '';
                    $(this).parent().parent().html(content);
                    location.reload()


                } else {
                    alert(data.errmsg)
                }
            }
        })
    })

    //TODO 添加地址
    $('.zcznmdfc1').click(function () {
        var source_text = $('.zuirifip').html();
        content = '<div class="adressdeliebi">';
        content += '<div class="adressfive">';
        content += '<div class="shrtm">';
        content += '<span>收货人</span>';
        content += '<input type="text" class="addr_name">';
        content += '</div>';
        content += '<div class="shrszdq">';
        content += '<span>所在地区</span>';
        content += '<input type="text" class="addr_area">';
        content += '</div>';
        content += '<div class="shrjddz">';
        content += '<span>街道地址</span>';
        content += '<input type="text" class="addr_detail">';
        content += '</div>';
        content += '<div class="shrjbdh">';
        content += '<span>联系方式</span>';
        content += '<input type="text" class="telephone">';
        content += '</div>';
        content += '<div class="shrtmdcz" style=" float:right">';
        content += '<span>操作</span>';
        content += '<a href="javascript:;" class="addr_append" onclick="append_addr()">添加</a>';
        content += '</div>';
        content += '</div>';
        content += '</div>';

        $('.zuirifip').html(source_text + content)
    })


})


// TODO 修改地址
function change_addr() {
    var addr_id = $('.addr_save').parent().parent().prev().html();
    var addr_name = $('.addr_name').val();
    var addr_area = $('.addr_area').val();
    var addr_detail = $('.addr_detail').val();
    var addr_mobile = $('.telephone').val();
    params = {
        'addr_id': addr_id,
        'addr_name': addr_name,
        'addr_area': addr_area,
        'addr_detail': addr_detail,
        'addr_mobile': addr_mobile,
    };
    $.ajax({
        url: '/user/save_addr',
        type: "post",
        headers: {
            "X-CSRFToken": getCookie("csrf_token")
        },
        contentType: "application/json",
        data: JSON.stringify(params),
        success: function (data) {
            if (data.errno == '0') {
                content = '<div class="shrtm">';
                content += '<span>收货人</span>';
                content += '<p>' + data.r_name + '</p>';
                content += '</div>';
                content += '<div class="shrszdq">';
                content += '<span>所在地区</span>';
                content += '<p>' + data.r_addr_area + '</p>';
                content += '</div>';
                content += '<div class="shrjddz">';
                content += '<span>街道地址</span>';
                content += '<p>' + data.r_addr + '</p>';
                content += '</div>';
                content += '<div class="shrjbdh">';
                content += '<span>联系方式</span>';
                content += '<p>' + data.r_mobile + '</p>';
                content += '</div>';
                content += '<div class="shrtmdcz" style=" float:right">';
                content += '<span>操作</span>';
                content += '<a href="javascript:;" class="change">编辑</a>';
                content += '<a href="javascript:;" class="remove">删除</a>';
                content += '</div>';
                $('.addr_save').parent().parent().html(content);
                alert(data.errmsg);
                location.reload()
            } else {
                alert(data.errmsg)
            }

        }, error() {
            alert('网络繁忙,请稍后再试!')
        }
    })


}

// TODO 添加地址
function append_addr() {
    var addr_name = $('.addr_name').val();
    var addr_area = $('.addr_area').val();
    var addr_detail = $('.addr_detail').val();
    var addr_mobile = $('.telephone').val();
    params = {
        'addr_name': addr_name,
        'addr_area': addr_area,
        'addr_detail': addr_detail,
        'addr_mobile': addr_mobile,
    };
    $.ajax({
        url: '/user/append_addr',
        type: "post",
        headers: {
            "X-CSRFToken": getCookie("csrf_token")
        },
        contentType: "application/json",
        data: JSON.stringify(params),
        success: function (data) {
            if (data.errno == '0') {
                content = '<div class="shrtm">';
                content += '<span>收货人</span>';
                content += '<p>' + data.r_name + '</p>';
                content += '</div>';
                content += '<div class="shrszdq">';
                content += '<span>所在地区</span>';
                content += '<p>' + data.r_addr_area + '</p>';
                content += '</div>';
                content += '<div class="shrjddz">';
                content += '<span>街道地址</span>';
                content += '<p>' + data.r_addr + '</p>';
                content += '</div>';
                content += '<div class="shrjbdh">';
                content += '<span>联系方式</span>';
                content += '<p>' + data.r_mobile + '</p>';
                content += '</div>';
                content += '<div class="shrtmdcz" style=" float:right">';
                content += '<span>操作</span>';
                content += '<a href="javascript:;" class="change">编辑</a>';
                content += '<a href="javascript:;" class="remove">删除</a>';
                content += '</div>';
                $('.addr_append').parent().parent().html(content);
                location.reload();
                alert(data.errmsg)
            } else {
                alert(data.errmsg)
            }

        }, error() {
            alert('网络繁忙,请稍后再试!')
        }
    })
}



function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}